package com.md.api.service.impl;

import com.md.api.common.utils.AliOssUtil;
import com.md.api.common.utils.DateUtils;
import com.md.api.common.utils.TokenUtil;
import com.md.api.dao.PhotoInfoMapper;
import com.md.api.dao.PictureInfoMapper;
import com.md.api.dao.RecycleBinMapper;
import com.md.api.entity.HttpResult;
import com.md.api.entity.PhotoInfo;
import com.md.api.entity.PictureInfo;
import com.md.api.entity.RecycleBin;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.enums.PhotoStatus;
import com.md.api.entity.enums.PictureStatus;
import com.md.api.entity.util.Page;
import com.md.api.service.RecycleBinSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author liujie
 * @ClassName RecycleBinServiceImpl
 **/

@Slf4j
@Service
public class RecycleBinServiceImpl implements RecycleBinSevice {


    @Resource
    TokenUtil tokenUtil;
    @Resource
    PhotoInfoMapper photoMapper;
    @Resource
    PictureInfoMapper pictureInfoMapper;
    @Resource
    RecycleBinMapper recycleBinMapper;



    @Transactional
    @Override
    public HttpResult recovery(HttpServletRequest request, ComposeCommitDto commitDto) {
        try {
            String userId = tokenUtil.getUserId(request);
            if (commitDto.getRecycleType()==1) {
                PhotoInfo dbPhotoInfo = photoMapper.getByPhotoId(commitDto.getPhotoId());
                dbPhotoInfo.setStatus(PhotoStatus.DELETED.value());
                photoMapper.updatePhoto(dbPhotoInfo);

                RecycleBin recycle = new RecycleBin();
                recycle.setUserId(userId);
                recycle.setName(dbPhotoInfo.getPhotoName());
                recycle.setRecycleType(commitDto.getRecycleType());
                recycle.setOnlyId(commitDto.getPhotoId());
                recycle.setDisplayImageUrl(dbPhotoInfo.getCoverImageUrl());
                recycle.setStatus(0);
                recycle.setInvalidTime(DateUtils.add(new Date(), Calendar.WEEK_OF_MONTH,2));
                recycleBinMapper.insert(recycle);
            }else{
                for (String pictureId :commitDto.getPictureIds()) {
                    PictureInfo dnPictureInfo = pictureInfoMapper.getByPictureId(pictureId);
                    dnPictureInfo.setStatus(PictureStatus.DELETED.value());
                    pictureInfoMapper.updatePicture(dnPictureInfo);

                    RecycleBin recycle = new RecycleBin();
                    recycle.setUserId(userId);
                    recycle.setRecycleType(commitDto.getRecycleType());
                    recycle.setOnlyId(pictureId);
                    recycle.setSize(dnPictureInfo.getSize());
                    recycle.setName(dnPictureInfo.getPictureName());
                    recycle.setDisplayImageUrl(dnPictureInfo.getPictureUrl());
                    recycle.setStatus(0);
                    recycle.setInvalidTime(DateUtils.add(new Date(), Calendar.WEEK_OF_MONTH,2));
                    recycleBinMapper.insert(recycle);
                }
            }
            return HttpResult.OK;
        }catch (Exception e){
            e.printStackTrace();
            return HttpResult.fail("回收失败！");
        }
    }

    @Override
    public HttpResult recoveryPage(HttpServletRequest request,ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(RecycleBin.class);
        String userId = tokenUtil.getUserId(request);
        List<RecycleBin> list = recycleBinMapper.getRecycleBinByUserId(userId,0);
        page.setResult(list);
        return new HttpResult(page);
    }

    @Transactional
    @Override
    public HttpResult restore(HttpServletRequest request, RecycleBin recycle) {
        try {
            recycle.setUserId(tokenUtil.getUserId(request));
            recycleBinMapper.delete(recycle);
            if (recycle.getRecycleType()==1) {
                PhotoInfo photoInfo = new PhotoInfo();
                photoInfo.setPhotoId(recycle.getOnlyId());
                photoInfo.setStatus(0);
                photoMapper.updatePhoto(photoInfo);
            }else{
                PictureInfo pictureInfo = new PictureInfo();
                pictureInfo.setPictureId(recycle.getOnlyId());
                pictureInfo.setStatus(0);
                pictureInfoMapper.updatePicture(pictureInfo);
            }
            return HttpResult.OK;
        }catch (Exception e){
            e.printStackTrace();
            return HttpResult.fail("恢复失败！");
        }
    }

    @Override
    public HttpResult remove(HttpServletRequest request, ComposeCommitDto dto) {
        recycleBinMapper.updateStatus(dto.getDeleteIds());
        return HttpResult.OK;
    }
}
